layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;

    /**
     * 系统管理--用户管理
     */
    var examMonth = {
        tableId: "examMonthTable",    //表格id
        condition: {
            name: "",
            deptId: "",
            timeLimit: ""
        }
    };

    /**
     * 初始化表格的列
     */
    examMonth.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'examMonthId', hide: true, sort: true, title: 'examMonthId'},
            {field: 'month', sort: true, title: '考试名称'},
            {field: 'soldOut', sort: true, title: '状态', templet: function (d) {
                    if (d.soldOut == 0) {
                        return "下架";
                    }else {
                        return "上架";
                    }
                }},
            {field: 'remarks', sort: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 150}
        ]];
    };

    /**
     * 弹出添加用户对话框
     */
    examMonth.openAddUser = function () {
        admin.putTempData('formOk', false);
        layer.full(layui.admin.open({
            type: 2,
            title: '添加',
            area: ['100%', '100%'],
            content: Feng.ctxPath + '/exam/examMonth_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(examMonth.tableId);
            }
        }));
    };

    /**
     * 点击编辑用户按钮时
     *
     * @param data 点击按钮时候的行数据
     */
    examMonth.onEditExam = function (data) {
        admin.putTempData('formOk', false);
        layer.full(layui.admin.open({
            type: 2,
            title: '编辑',
            area: ['100%', '100%'],
            content: Feng.ctxPath + '/exam/examMonth_edit?examMonthId=' + data.examMonthId ,
            end: function () {
                admin.getTempData('formOk') && table.reload(examMonth.tableId);
            }
        }));
    };
    examMonth.onDeleteExam = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/exam/examMonthDelete", function () {
                table.reload(examMonth.tableId);
                Feng.success("删除成功!");
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("examMonthId", data.examMonthId);
            ajax.start();
        };
        Feng.confirm("是否删除" + data.month + "?", operation);
    }

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + examMonth.tableId,
        url: Feng.ctxPath + '/exam/examMonthList',
        page: true,
        height: "full-98",
        cellMinWidth: 120,
        cols: examMonth.initColumn()
    });


    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        examMonth.openAddUser();
    });


    // 工具条点击事件
    table.on('tool(' + examMonth.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            examMonth.onEditExam(data);
        }else if (layEvent === 'delete') {
            examMonth.onDeleteExam(data);
        }
    });

});
